home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / mpeg2decode / src / mpegaudio / common.c next >
C/C++ Source or Header  |  1999-03-02  |  26KB  |  783 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com             *
  10.  *                                                                    *
  11.  * VERSION 4.0                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Created this file for all common      *
  17.  *                              functions and global variables.       *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added Jean-Georges Fritsch's          *
  20.  *                              "bitstream.c" package.                *
  21.  *                              Added routines to handle AIFF PCM     *
  22.  *                              sound files.                          *
  23.  *                              Added "mem_alloc()" and "mem_free()"  *
  24.  *                              routines for memory allocation        *
  25.  *                              portability.                          *
  26.  *                              Added routines to convert between     *
  27.  *                              Apple SANE extended floating point    *
  28.  *                              format and IEEE double precision      *
  29.  *                              floating point format.  For AIFF.     *
  30.  * 02jul91 dpwe (Aware Inc)     Moved allocation table input here;    *
  31.  *                              Tables read from subdir TABLES_PATH.  *
  32.  *                              Added some debug printout fns (Write*)*
  33.  * 7/10/91 Earle Jennings       replacement of the one float by FLOAT *
  34.  *                              port to MsDos from MacIntosh version  *
  35.  * 8/ 5/91 Jean-Georges Fritsch fixed bug in open_bit_stream_r()      *
  36.  *10/ 1/91 S.I. Sudharsanan,    Ported to IBM AIX platform.           *
  37.  *         Don H. Lee,                                                *
  38.  *         Peter W. Farrett                                           *
  39.  *10/3/91  Don H. Lee           implemented CRC-16 error protection   *
  40.  *                              newly introduced functions are        *
  41.  *                              I_CRC_calc, II_CRC_calc and           *
  42.  *                              update_CRC. Additions and revisions   *
  43.  *                              are marked with dhl for clarity       *
  44.  *10/18/91 Jean-Georges Fritsch fixed bug in update_CRC(),            *
  45.  *                              II_CRC_calc() and I_CRC_calc()        *
  46.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  47.  *                              important fixes involved changing     *
  48.  *                              16-bit ints to long or unsigned in    *
  49.  *                              bit alloc routines for quant of 65535 *
  50.  *                              and passing proper function args.     *
  51.  *                              Removed "Other Joint Stereo" option   *
  52.  *                              and made bitrate be total channel     *
  53.  *                              bitrate, irrespective of the mode.    *
  54.  *                              Fixed many small bugs & reorganized.  *
  55.  * 3/20/92 Jean-Georges Fritsch  fixed bug in start-of-frame search   *
  56.  * 6/15/92 Juan Pineda          added refill_buffer(bs) "n"           *
  57.  *                              initialization                        *
  58.  * 7/08/92 Susanne Ritscher     MS-DOS, MSC6.0 port fixes             *
  59.  * 7/27/92 Mike Li               (re-)Port to MS-DOS                  *
  60.  * 8/19/92 Soren H. Nielsen     Fixed bug in I_CRC_calc and in        *
  61.  *                              II_CRC_calc.  Added function: new_ext *
  62.  *                              for better MS-DOS compatability       *
  63.  * 3/10/93 Kevin Peterson       changed aiff_read_headers to handle   *
  64.  *                              chunks in any order.  now returns     *
  65.  *                              position of sound data in file.       *
  66.  * 3/31/93 Jens Spille          changed IFF_* string compares to use  *
  67.  *                              strcmp()                              *
  68.  * 5/30/93 Masahiro Iwadare ?? the previous modification does not *
  69.  *                work. recovered to the original. ?? *
  70.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  71.  *         Daniel Lauzon, and                                         *
  72.  *         Bill Truerniet                                             *
  73.  **********************************************************************/
  74.  
  75. /***********************************************************************
  76. *
  77. *  Global Include Files
  78. *
  79. ***********************************************************************/
  80.  
  81. #include        "common.h"
  82.  
  83. #ifdef  MACINTOSH
  84.  
  85. #include        <SANE.h>
  86. #include        <pascal.h>
  87.  
  88. #endif
  89.  
  90. #include <ctype.h>
  91. #ifdef STORM
  92. #include "/audio.h"
  93. #else
  94. #include "audio.h"
  95. #endif
  96.  
  97. /***********************************************************************
  98. *
  99. *  Global Variable Definitions
  100. *
  101. ***********************************************************************/
  102.  
  103. char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };
  104. char *layer_names[3] = { "I", "II", "III" };
  105.  
  106. double  s_freq[4] = {44.1, 48, 32, 0};
  107.  
  108. int     bitrate[3][15] = {
  109.           {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  110.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  111.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
  112.         };
  113.  
  114. double FAR multiple[64] = {
  115. 2.00000000000000, 1.58740105196820, 1.25992104989487,
  116. 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
  117. 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
  118. 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
  119. 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
  120. 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
  121. 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
  122. 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
  123. 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
  124. 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
  125. 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
  126. 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
  127. 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
  128. 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
  129. 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
  130. 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
  131. 1E-20
  132. };
  133.  
  134. struct alloc_entry
  135. {
  136. unsigned int    steps;
  137. unsigned int    bits;
  138. unsigned int    group;
  139. unsigned int    quant;
  140. };
  141.  
  142. #include "tables/alloc_0.h"
  143. #include "tables/alloc_1.h"
  144. #include "tables/alloc_2.h"
  145. #include "tables/alloc_3.h"
  146.  
  147. /***********************************************************************
  148. *
  149. *  Global Function Definitions
  150. *
  151. ***********************************************************************/
  152.  
  153. /* The system uses a variety of data files.  By opening them via this
  154.    function, we can accommodate various locations. */
  155.  
  156. /***********************************************************************
  157. /*
  158. /* Read one of the data files ("alloc_*") specifying the bit allocation/
  159. /* quatization parameters for each subband in layer II encoding
  160. /*
  161. /**********************************************************************/
  162.  
  163. /***********************************************************************
  164. /*
  165. /* Using the decoded info the appropriate possible quantization per
  166. /* subband table is loaded
  167. /*
  168. /**********************************************************************/
  169.  
  170. #ifdef STORM
  171. int pick_table(frame_params *fr_ps)
  172. #else
  173. int pick_table(fr_ps)   /* choose table, load if necess, return # sb's */
  174. frame_params *fr_ps;
  175. #endif
  176. {
  177.         int table, lay, ws, bsp, br_per_ch, sfrq;
  178.         int sblim = fr_ps->sblimit;     /* return current value if no load */
  179.  
  180.         lay = fr_ps->header->lay - 1;
  181.         bsp = fr_ps->header->bitrate_index;
  182.         br_per_ch = bitrate[lay][bsp] / fr_ps->stereo;
  183.         ws = fr_ps->header->sampling_frequency;
  184.         sfrq = s_freq[ws];
  185.         /* decision rules refer to per-channel bitrates (kbits/sec/chan) */
  186.         if ((sfrq == 48 && br_per_ch >= 56) ||
  187.             (br_per_ch >= 56 && br_per_ch <= 80)) table = 0;
  188.         else if (sfrq != 48 && br_per_ch >= 96) table = 1;
  189.         else if (sfrq != 32 && br_per_ch <= 48) table = 2;
  190.         else table = 3;
  191.         if (fr_ps->tab_num != table) {
  192.  
  193.         switch((fr_ps->tab_num = table))
  194.         {
  195.             default:
  196.             case 0:
  197.                 printf("using bit-allocation table 0.\n");
  198. #ifdef STORM
  199.                 fr_ps->alloc = (void *)&t_alloc_0;
  200. #else
  201.                 fr_ps->alloc = &t_alloc_0;
  202. #endif
  203.                 sblim = 27;
  204.             break;
  205.             case 1:
  206.                 printf("using bit-allocation table 1.\n");
  207. #ifdef STORM
  208.                 fr_ps->alloc = (void *)&t_alloc_1;
  209. #else
  210.                 fr_ps->alloc = &t_alloc_1;
  211. #endif
  212.                 sblim = 30;
  213.             break;
  214.             case 2:
  215.                 printf("using bit-allocation table 2.\n");
  216. #ifdef STORM
  217.                 fr_ps->alloc = (void *)&t_alloc_2;
  218. #else
  219.                 fr_ps->alloc = &t_alloc_2;
  220. #endif
  221.                 sblim = 8;
  222.             break;
  223.             case 3:
  224.                 printf("using bit-allocation table 3.\n");
  225. #ifdef STORM
  226.                 fr_ps->alloc = (void *)&t_alloc_3;
  227. #else
  228.                 fr_ps->alloc = &t_alloc_3;
  229. #endif
  230.                 sblim = 12;
  231.             break;
  232.         }
  233.         }
  234.         return sblim;
  235. }
  236.  
  237. #ifdef STORM
  238. int js_bound(int lay, int m_ext)
  239. #else
  240. int js_bound(lay, m_ext)
  241. int lay, m_ext;
  242. #endif
  243. {
  244. static int jsb_table[3][4] =  { { 4, 8, 12, 16 }, { 4, 8, 12, 16},
  245.                                 { 0, 4, 8, 16} };  /* lay+m_e -> jsbound */
  246.  
  247.     if(lay<1 || lay >3 || m_ext<0 || m_ext>3) {
  248.         fprintf(stderr, "js_bound bad layer/modext (%d/%d)\n", lay, m_ext);
  249.         exit(1);
  250.     }
  251.     return(jsb_table[lay-1][m_ext]);
  252. }
  253.  
  254. #ifdef STORM
  255. void hdr_to_frps(frame_params *fr_ps)
  256. #else
  257. void hdr_to_frps(fr_ps) /* interpret data in hdr str to fields in fr_ps */
  258. frame_params *fr_ps;
  259. #endif
  260. {
  261. layer *hdr = fr_ps->header;     /* (or pass in as arg?) */
  262.  
  263.     fr_ps->actual_mode = hdr->mode;
  264.     fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
  265.     if (hdr->lay == 2)          fr_ps->sblimit = pick_table(fr_ps);
  266.     else                        fr_ps->sblimit = SBLIMIT;
  267.     if(hdr->mode == MPG_MD_JOINT_STEREO)
  268.         fr_ps->jsbound = js_bound(hdr->lay, hdr->mode_ext);
  269.     else
  270.         fr_ps->jsbound = fr_ps->sblimit;
  271.     /* alloc, tab_num set in pick_table */
  272. }
  273.  
  274. /*******************************************************************************
  275. *
  276. *  Allocate number of bytes of memory equal to "block".
  277. *
  278. *******************************************************************************/
  279.  
  280. #ifdef STORM
  281. void  FAR *mem_alloc(unsigned long block, char *item)
  282. #else
  283. void  FAR *mem_alloc(block, item)
  284. unsigned long   block;
  285. char            *item;
  286. #endif
  287. {
  288.  
  289.     void    *ptr;
  290.  
  291. #ifdef  MACINTOSH
  292.     ptr = NewPtr(block);
  293. #endif
  294.  
  295. #ifdef MSC60
  296.     /*ptr = (void FAR *) _fmalloc((unsigned int)block);*/ /* far memory, 92-07-08 sr */
  297.     ptr = (void FAR *) malloc((unsigned int)block); /* far memory, 93-08-24 ss */
  298. #endif
  299.  
  300. #if ! defined (MACINTOSH) && ! defined (MSC60)
  301.     ptr = (void FAR *) malloc(block);
  302. #endif
  303.  
  304.     if (ptr != NULL){
  305. #ifdef  MSC60
  306.         _fmemset(ptr, 0, (unsigned int)block); /* far memory, 92-07-08 sr */
  307. #else
  308.         memset(ptr, 0, block);
  309. #endif
  310.     }
  311.     else{
  312.         printf("Unable to allocate %s\n", item);
  313.         exit(0);
  314.     }
  315.     return(ptr);
  316. }
  317.  
  318.  
  319. /****************************************************************************
  320. *
  321. *  Free memory pointed to by "*ptr_addr".
  322. *
  323. *****************************************************************************/
  324.  
  325. #ifdef STORM
  326. void    mem_free(void **ptr_addr)
  327. #else
  328. void    mem_free(ptr_addr)
  329. void    **ptr_addr;
  330. #endif
  331. {
  332.  
  333.     if (*ptr_addr != NULL){
  334. #ifdef  MACINTOSH
  335.         DisposPtr(*ptr_addr);
  336. #else
  337.         free(*ptr_addr);
  338. #endif
  339.         *ptr_addr = NULL;
  340.     }
  341.  
  342. }
  343.  
  344. /*******************************************************************************
  345. *
  346. *  Check block of memory all equal to a single byte, else return FALSE
  347. *
  348. *******************************************************************************/
  349.  
  350. #ifdef STORM
  351. int memcheck(char *array, int test, int num)
  352. #else
  353. int memcheck(array, test, num)
  354. char *array;
  355. int test;       /* but only tested as a char (bottom 8 bits) */
  356. int num;
  357. #endif
  358. {
  359.  int i=0;
  360.  
  361.    while (array[i] == test && i<num) i++;
  362.    if (i==num) return TRUE;
  363.    else return FALSE;
  364. }
  365.  
  366. /****************************************************************************
  367. *
  368. *  Routines to convert between the Apple SANE extended floating point format
  369. *  and the IEEE double precision floating point format.  These routines are
  370. *  called from within the Audio Interchange File Format (AIFF) routines.
  371. *
  372. *****************************************************************************/
  373.  
  374. /*
  375. *** Apple's 80-bit SANE extended has the following format:
  376.  
  377.  1       15      1            63
  378. +-+-------------+-+-----------------------------+
  379. |s|       e     |i|            f                |
  380. +-+-------------+-+-----------------------------+
  381.   msb        lsb   msb                       lsb
  382.  
  383. The value v of the number is determined by these fields as follows:
  384. If 0 <= e < 32767,              then v = (-1)^s * 2^(e-16383) * (i.f).
  385. If e == 32767 and f == 0,       then v = (-1)^s * (infinity), regardless of i.
  386. If e == 32767 and f != 0,       then v is a NaN, regardless of i.
  387.  
  388. *** IEEE Draft Standard 754 Double Precision has the following format:
  389.  
  390. MSB
  391. +-+---------+-----------------------------+
  392. |1| 11 Bits |           52 Bits           |
  393. +-+---------+-----------------------------+
  394.  ^     ^                ^
  395.  |     |                |
  396.  Sign  Exponent         Mantissa
  397. */
  398.  
  399. /****  for debugging 
  400. showchar(str)
  401. char str[4];
  402. {
  403. int i;
  404. for (i=0;i<4;i++) printf("%c",str[i]);
  405. printf("\n");
  406. }
  407. ****/
  408.  
  409. /*****************************************************************************
  410. *
  411. *  bit_stream.c package
  412. *  Author:  Jean-Georges Fritsch, C-Cube Microsystems
  413. *
  414. *****************************************************************************/
  415.  
  416. /********************************************************************
  417.   This package provides functions to write (exclusive or read)
  418.   information from (exclusive or to) the bit stream.
  419.  
  420.   If the bit stream is opened in read mode only the get functions are
  421.   available. If the bit stream is opened in write mode only the put
  422.   functions are available.
  423. ********************************************************************/
  424.  
  425. /*open_bit_stream_r(); open the device to read the bit stream from it     */
  426. /*close_bit_stream();  close the device containing the bit stream         */
  427. /*alloc_buffer();      open and initialize the buffer;                    */
  428. /*desalloc_buffer();   empty and close the buffer                         */
  429. /*unsigned int get1bit();  read 1 bit from the bit stream                 */
  430. /*unsigned long getbits(); read N bits from the bit stream                */
  431. /*unsigned long look_ahead(); grep the next N bits in the bit stream without*/
  432. /*                            changing the buffer pointer                   */
  433. /*unsigned long sstell(); return the current bit stream length (in bits)    */
  434. /*int seek_sync(); return 1 if a sync word was found in the bit stream      */
  435. /*                 otherwise returns 0                                      */
  436.  
  437. /* refill the buffer from the input device when the buffer becomes empty    */
  438. #ifdef STORM
  439. int refill_buffer(Bit_stream_struc *bs)
  440. #else
  441. int refill_buffer(bs)
  442. Bit_stream_struc *bs;   /* bit stream structure */
  443. #endif
  444. {
  445.    register int i=bs->buf_size-2-bs->buf_byte_idx;
  446.    register unsigned long n=1;
  447.    register int index=0;
  448.    char val[2];
  449.  
  450.    while ((i>=0) && (!bs->eob))
  451.     bs->buf[i--] = audio_pollbuffer();
  452. }
  453.  
  454. static char *he = "0123456789ABCDEF";
  455.  
  456.  
  457. /* open the device to read the bit stream from it */
  458. #ifdef STORM
  459. void open_bit_stream_r(Bit_stream_struc *bs, char *bs_filenam, int size)
  460. #else
  461. void open_bit_stream_r(bs, bs_filenam, size)
  462. Bit_stream_struc *bs;   /* bit stream structure */
  463. char *bs_filenam;       /* name of the bit stream file */
  464. int size;               /* size of the buffer */
  465. #endif
  466. {
  467.    bs->format = BINARY;
  468.    alloc_buffer(bs, size);
  469.    bs->buf_byte_idx=0;
  470.    bs->buf_bit_idx=0;
  471.    bs->totbit=0;
  472.    bs->mode = READ_MODE;
  473.    bs->eob = FALSE;
  474.    bs->eobs = FALSE;
  475. }
  476.  
  477. /*close the device containing the bit stream after a read process*/
  478. #ifdef STORM
  479. void close_bit_stream_r(Bit_stream_struc *bs)
  480. #else
  481. void close_bit_stream_r(bs)
  482. Bit_stream_struc *bs;   /* bit stream structure */
  483. #endif
  484. {
  485.    desalloc_buffer(bs);
  486. }
  487.  
  488. /*close the device containing the bit stream after a write process*/
  489. #ifdef STORM
  490. void close_bit_stream_w(Bit_stream_struc *bs)
  491. #else
  492. void close_bit_stream_w(bs)
  493. Bit_stream_struc *bs;   /* bit stream structure */
  494. #endif
  495. {
  496.    desalloc_buffer(bs);
  497. }
  498.  
  499. /*open and initialize the buffer; */
  500. #ifdef STORM
  501. void alloc_buffer(Bit_stream_struc *bs, int size)
  502. #else
  503. void alloc_buffer(bs, size)
  504. Bit_stream_struc *bs;   /* bit stream structure */
  505. int size;
  506. #endif
  507. {
  508.    bs->buf = (unsigned char FAR *) mem_alloc(size*sizeof(unsigned
  509.               char), "buffer");
  510.    bs->buf_size = size;
  511. }
  512.  
  513. /*empty and close the buffer */
  514. #ifdef STORM
  515. void desalloc_buffer(Bit_stream_struc *bs)
  516. #else
  517. void desalloc_buffer(bs)
  518. Bit_stream_struc *bs;   /* bit stream structure */
  519. #endif
  520. {
  521.    free(bs->buf);
  522. }
  523.  
  524. int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};
  525. int clearmask[9]={0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x0};
  526.  
  527. int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
  528.  
  529. /*read 1 bit from the bit stream */
  530. #ifdef STORM
  531. unsigned int get1bit(Bit_stream_struc *bs)
  532. #else
  533. unsigned int get1bit(bs)
  534. Bit_stream_struc *bs;   /* bit stream structure */
  535. #endif
  536. {
  537.    unsigned int bit;
  538.    register int i;
  539.  
  540.    bs->totbit++;
  541.  
  542.    if (!bs->buf_bit_idx) {
  543.         bs->buf_bit_idx = 8;
  544.         bs->buf_byte_idx--;
  545.         if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
  546.              if (bs->eob)
  547.                 bs->eobs = TRUE;
  548.              else {
  549.                 for (i=bs->buf_byte_idx; i>=0;i--)
  550.                   bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
  551.                 refill_buffer(bs);
  552.                 bs->buf_byte_idx = bs->buf_size-1;
  553.              }
  554.         }
  555.    }
  556.    bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];
  557.    bit = bit >> (bs->buf_bit_idx-1);
  558.    bs->buf_bit_idx--;
  559.    return(bit);
  560. }
  561.  
  562. /*look ahead for the next N bits from the bit stream */
  563. #ifdef STORM
  564. unsigned long look_ahead(Bit_stream_struc *bs, int N)
  565. #else
  566. unsigned long look_ahead(bs, N)
  567. Bit_stream_struc *bs;   /* bit stream structure */
  568. int N;                  /* number of bits to read from the bit stream */
  569. #endif
  570. {
  571.  unsigned long val=0;
  572.  register int j = N;
  573.  register int k, tmp;
  574.  register int bit_idx = bs->buf_bit_idx;
  575.  register int byte_idx = bs->buf_byte_idx;
  576.  
  577.  if (N > MAX_LENGTH)
  578.     printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
  579.  
  580.  while (j > 0) {
  581.     if (!bit_idx) {
  582.         bit_idx = 8;
  583.         byte_idx--;
  584.     }
  585.     k = MIN (j, bit_idx);
  586.     tmp = bs->buf[byte_idx]&putmask[bit_idx];
  587.     tmp = tmp >> (bit_idx-k);
  588.     val |= tmp << (j-k);
  589.     bit_idx -= k;
  590.     j -= k;
  591.  }
  592.  return(val);
  593. }
  594.  
  595. /*read N bit from the bit stream */
  596. #ifdef STORM
  597. unsigned long getbits(Bit_stream_struc *bs, int N)
  598. #else
  599. unsigned long getbits(bs, N)
  600. Bit_stream_struc *bs;   /* bit stream structure */
  601. int N;                  /* number of bits to read from the bit stream */
  602. #endif
  603. {
  604.  unsigned long val=0;
  605.  register int i;
  606.  register int j = N;
  607.  register int k, tmp;
  608.  
  609.  if (N > MAX_LENGTH)
  610.     printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
  611.  
  612.  bs->totbit += N;
  613.  while (j > 0) {
  614.    if (!bs->buf_bit_idx) {
  615.         bs->buf_bit_idx = 8;
  616.         bs->buf_byte_idx--;
  617.         if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
  618.              if (bs->eob)
  619.                 bs->eobs = TRUE;
  620.              else {
  621.                 for (i=bs->buf_byte_idx; i>=0;i--)
  622.                    bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
  623.                 refill_buffer(bs);
  624.                 bs->buf_byte_idx = bs->buf_size-1;
  625.              }
  626.         }
  627.    }
  628.    k = MIN (j, bs->buf_bit_idx);
  629.    tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];
  630.    tmp = tmp >> (bs->buf_bit_idx-k);
  631.    val |= tmp << (j-k);
  632.    bs->buf_bit_idx -= k;
  633.    j -= k;
  634.  }
  635.  return(val);
  636. }
  637.  
  638. /*return the current bit stream length (in bits)*/
  639. #ifdef STORM
  640. unsigned long sstell(Bit_stream_struc *bs)
  641. #else
  642. unsigned long sstell(bs)
  643. Bit_stream_struc *bs;   /* bit stream structure */
  644. #endif
  645. {
  646.   return(bs->totbit);
  647. }
  648.  
  649. /*this function seeks for a byte aligned sync word in the bit stream and
  650.   places the bit stream pointer right after the sync.
  651.   This function returns 1 if the sync was found otherwise it returns 0  */
  652. #ifdef STORM
  653. int seek_sync(Bit_stream_struc *bs, long sync, int N)
  654. #else
  655. int seek_sync(bs, sync, N)
  656. Bit_stream_struc *bs;   /* bit stream structure */
  657. long sync;      /* sync word maximum 32 bits */
  658. int N;          /* sync word length */
  659. #endif
  660. {
  661.  double pow();
  662.  unsigned long aligning, stell();
  663.  unsigned long val;
  664.  long maxi = (int)pow(2.0, (FLOAT)N) - 1;
  665.  
  666.  aligning = sstell(bs)%ALIGNING;
  667.  if (aligning)
  668.     getbits(bs, (int)(ALIGNING-aligning));
  669.  
  670.   val = getbits(bs, N);
  671.   while (((val&maxi) != sync) && (audioBufferSize>0)) {
  672.         val <<= ALIGNING;
  673.         val |= getbits(bs, ALIGNING);
  674.   }
  675.  
  676.  if (!audioBufferSize) return(0);
  677.  else return(1);
  678. }
  679. /*****************************************************************************
  680. *
  681. *  End of bit_stream.c package
  682. *
  683. *****************************************************************************/
  684.  
  685. /*****************************************************************************
  686. *
  687. *  CRC error protection package
  688. *
  689. *****************************************************************************/
  690.  
  691. #ifdef STORM
  692. void I_CRC_calc(frame_params *fr_ps, unsigned int bit_alloc[2][SBLIMIT], unsigned int *crc)
  693. #else
  694. void I_CRC_calc(fr_ps, bit_alloc, crc)
  695. frame_params *fr_ps;
  696. unsigned int bit_alloc[2][SBLIMIT];
  697. unsigned int *crc;
  698. #endif
  699. {
  700.         int i, k;
  701.         layer *info = fr_ps->header;
  702.         int stereo  = fr_ps->stereo;
  703.         int jsbound = fr_ps->jsbound;
  704.  
  705.         *crc = 0xffff; /* changed from '0' 92-08-11 shn */
  706.         update_CRC(info->bitrate_index, 4, crc);
  707.         update_CRC(info->sampling_frequency, 2, crc);
  708.         update_CRC(info->padding, 1, crc);
  709.         update_CRC(info->extension, 1, crc);
  710.         update_CRC(info->mode, 2, crc);
  711.         update_CRC(info->mode_ext, 2, crc);
  712.         update_CRC(info->copyright, 1, crc);
  713.         update_CRC(info->original, 1, crc);
  714.         update_CRC(info->emphasis, 2, crc);
  715.  
  716.         for (i=0;i<SBLIMIT;i++)
  717.                 for (k=0;k<((i<jsbound)?stereo:1);k++)
  718.                         update_CRC(bit_alloc[k][i], 4, crc);
  719. }
  720.  
  721. #ifdef STORM
  722. void II_CRC_calc(frame_params *fr_ps, unsigned int bit_alloc[2][SBLIMIT], unsigned int scfsi[2][SBLIMIT], unsigned int *crc)
  723. #else
  724. void II_CRC_calc(fr_ps, bit_alloc, scfsi, crc)
  725. frame_params *fr_ps;
  726. unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
  727. unsigned int *crc;
  728. #endif
  729. {
  730.         int i, k;
  731.         layer *info = fr_ps->header;
  732.         int stereo  = fr_ps->stereo;
  733.         int sblimit = fr_ps->sblimit;
  734.         int jsbound = fr_ps->jsbound;
  735.         al_table *alloc = fr_ps->alloc;
  736.  
  737.         *crc = 0xffff; /* changed from '0' 92-08-11 shn */
  738.         update_CRC(info->bitrate_index, 4, crc);
  739.         update_CRC(info->sampling_frequency, 2, crc);
  740.         update_CRC(info->padding, 1, crc);
  741.         update_CRC(info->extension, 1, crc);
  742.         update_CRC(info->mode, 2, crc);
  743.         update_CRC(info->mode_ext, 2, crc);
  744.         update_CRC(info->copyright, 1, crc);
  745.         update_CRC(info->original, 1, crc);
  746.         update_CRC(info->emphasis, 2, crc);
  747.  
  748.         for (i=0;i<sblimit;i++)
  749.                 for (k=0;k<((i<jsbound)?stereo:1);k++)
  750.                         update_CRC(bit_alloc[k][i], (*alloc)[i][0].bits, crc);
  751.  
  752.         for (i=0;i<sblimit;i++)
  753.                 for (k=0;k<stereo;k++)
  754.                         if (bit_alloc[k][i])
  755.                                 update_CRC(scfsi[k][i], 2, crc);
  756. }
  757.  
  758. #ifdef STORM
  759. void update_CRC(unsigned int data, unsigned int length, unsigned int *crc)
  760. #else
  761. void update_CRC(data, length, crc)
  762. unsigned int data, length, *crc;
  763. #endif
  764. {
  765.         unsigned int  masking, carry;
  766.  
  767.         masking = 1 << length;
  768.  
  769.         while((masking >>= 1)){
  770.                 carry = *crc & 0x8000;
  771.                 *crc <<= 1;
  772.                 if (!carry ^ !(data & masking))
  773.                         *crc ^= CRC16_POLYNOMIAL;
  774.         }
  775.         *crc &= 0xffff;
  776. }
  777.  
  778. /*****************************************************************************
  779. *
  780. *  End of CRC error protection package
  781. *
  782. *****************************************************************************/
  783.